package com.ttjj.enums;

import com.alibaba.fastjson.annotation.JSONField;
import com.ttjj.entity.player.PlayerStateVo;
import org.testng.collections.Sets;

import java.util.List;
import java.util.Set;

/**
 * 动物类型
 */
public enum EnumAnimalType {
    Unknown(0),
    Chicken(1),
    Rabbit(2),
    Pig(3),
    Cattle(4);

    private int value;

    /**
     * 权重
     */
    private Double gold;

    EnumAnimalType(int value) {
        this.value = value;
    }

    @JSONField(serialize = false)
    public int get() {
        return this.value;
    }

    /**
     * 每种动物权重
     *
     * @return
     */
    public Double getWeight(PlayerStateVo stateVo, boolean isIn2min) {
        List<EnumAnimalType> delivered = stateVo.getDelivered();
        Set<EnumAnimalType> animalTypes = Sets.newHashSet(delivered);
        if (isIn2min) {
            return 1d;
        }
        Double weight;
        if (this == EnumAnimalType.Cattle) {
            weight = 1d + calculateOnlyNeed(animalTypes, EnumAnimalType.Cattle, isIn2min);
        } else if (this == EnumAnimalType.Rabbit) {
            weight = 1.57d + calculateOnlyNeed(animalTypes, EnumAnimalType.Rabbit, isIn2min);
        } else if (this == EnumAnimalType.Pig) {
            weight = 2.22d + calculateOnlyNeed(animalTypes, EnumAnimalType.Pig, isIn2min);
        } else {
            weight = 3.677d + calculateOnlyNeed(animalTypes, EnumAnimalType.Chicken, isIn2min);
        }

        return weight;
    }

    /**
     * 判断是否缺少
     *
     * @return
     */
    private Double calculateOnlyNeed(Set<EnumAnimalType> hasAnimalType, EnumAnimalType enumAnimalType, boolean isIn2min) {
        if (isIn2min) {
            // 2min 内先不操作
            return 0d;
        }
        int noHashType = 4 - hasAnimalType.size();
        if (!hasAnimalType.contains(enumAnimalType)) {
            return 110d / ((50d / noHashType) * getByType(enumAnimalType));
        }
        return 110d / ((50d / 4) * getByType(enumAnimalType));
    }

    /**
     * 获取分数
     *
     * @return
     */
    public Integer getByType(EnumAnimalType animalType) {
        switch (animalType) {
            case Chicken:
                return 30;
            case Pig:
                return 50;
            case Rabbit:
                return 70;
            case Cattle:
                return 110;
            default:
                return 0;
        }
    }
}
